﻿using System;
using System.Data.Entity;
using System.Linq;

namespace NQueries.Sample
{
    public static class Test
    {
        public static void CreateDatabase()
        {
            using (IUnitOfWork unitOfWork = UnitOfWork.Begin())
            {
                DbContext context = ((DbContext)UnitOfWork.CurrentDataContext);

                if (context.Database.Exists())
                    context.Database.Delete();

                context.Database.Create();

                Customer customer = new Customer { Name = "test", Address = new Address { Street = "test" } };
                unitOfWork.DataContext().Customers.Add(customer);
                unitOfWork.Commit();
            }
        }

        public static void CreateCustomer(string name)
        {
            using (IUnitOfWork unitOfWork = UnitOfWork.Begin())
            {
                Customer customer = new Customer { Name = name, Address = new Address { Street = name, Country = new Country { Name = name } } };
                customer.Companies.Add(new Company { Name = name });
                unitOfWork.DataContext().Customers.Add(customer);
                unitOfWork.Commit();
            }
        }

        public static void CustomerExists(string name)
        {
            using (IUnitOfWork unitOfWork = UnitOfWork.Begin())
                if (unitOfWork.DataContext().Customers.Count(x => x.Name == name) != 1)
                    throw new Exception("test failed");
        }

        public static void AddressExists(string street)
        {
            using (IUnitOfWork unitOfWork = UnitOfWork.Begin())
                if (unitOfWork.DataContext().Addresses.Count(x => x.Street == street) != 1)
                    throw new Exception("test failed");
        }

        public static void CustomerNotExists(string name)
        {
            using (IUnitOfWork unitOfWork = UnitOfWork.Begin())
                if (unitOfWork.DataContext().Customers.Any(x => x.Name == name))
                    throw new Exception("test failed");
        }

        public static void AddressNotExists(string street)
        {
            using (IUnitOfWork unitOfWork = UnitOfWork.Begin())
                if (unitOfWork.DataContext().Addresses.Any(x => x.Street == street))
                    throw new Exception("test failed");
        }
    }
}